package com.hq.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hq.entity.*;
import com.hq.mapper.ExamMapper;
import com.hq.mapper.QuestionMapper;
import com.hq.service.QuestionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("questionService")
@Slf4j
public class QuestionServiceImpl implements QuestionService {

    @Autowired
    private QuestionMapper questionMapper;

    @Autowired
    private ExamMapper examMapper;

    //封装在线考试数据
    @Override
    public Map<String, Object> findOnline() {
        Map<String,Object> data=new HashMap<>();
        //查询发布的试卷
        Exam exam = examMapper.findOnline();
        data.put("title",exam.getEname());
        data.put("eid",exam.getEid());
        data.put("etime",exam.getEtime());
        List<ExamBean> elist=new ArrayList<>();
        //查询试卷下所有的题目信息(1，单选 2，多选 3，判断)
        for(int i=1;i<=3;i++){
            ExamBean examBean=new ExamBean();
            //给examBean设置title
            examBean.setTitle(i==1?"单选选择题":(i==2?"多选选择器":"判断题"));
            List<TQuestion> tlist = questionMapper.findByType(exam.getEid(), i);
            List<QuestionBean> qlist=new ArrayList<>();
            for(TQuestion tQuestion:tlist){
                QuestionBean questionBean=new QuestionBean();
                questionBean.setType(tQuestion.getTid());
                questionBean.setQuestionStem(tQuestion.getQuestionStem());
                questionBean.setAnswer(tQuestion.getAnswer().split(","));
                questionBean.setOptions(tQuestion.getOptions().split(","));
                questionBean.setAnalysis(null);
                qlist.add(questionBean);
            }
            examBean.setInfos(null);
            examBean.setValues(qlist);
            elist.add(examBean);
        }
        data.put("exam",elist);
        return data;
    }


    //根据id查询试卷
    @Override
    public QuestionPo findById(Integer qid) {
        log.info("根据id查询试卷：{}",qid);
        return questionMapper.findById(qid);
    }

    //单行删除试卷
    @Override
    public int delQuestion(int qid) {
        log.info("删除试卷：{}",qid);
        return questionMapper.delQuestion(qid);
    }

    //试卷分页查询
    @Override
    public Map<String, Object> findByPage(int page, int size) {
        Map<String,Object> map=new HashMap<>();
        Page<Object> objects = PageHelper.startPage(page, size);
        List<Questions> list = questionMapper.findAll();
        map.put("list",list);
        map.put("total",objects.getTotal());
        return map;
    }

    //录入试卷
    @Override
    public int addQuestion(QuestionVo question) {
        log.info("录入试卷：{}",question);
        String options="";
        if(question.getOptions().length>0) {
            options = question.getOptions()[0];
            for (int i = 1; i < question.getOptions().length; i++) {
                options += "," + question.getOptions()[i];
            }
        }

        String answer="";
        if(question.getAnswer() instanceof String){ //单选
            answer=(String)question.getAnswer();
        }else if(question.getAnswer() instanceof  Integer) { //判断
            answer=(int)question.getAnswer()+"";
        }else{
            List list=(List)question.getAnswer();
            answer=list.get(0).toString();
            for(int i=1;i<list.size();i++){
                answer+=","+list.get(i).toString();
            }
        }
        return questionMapper.addQuestion(question.getTid(),question.getEid(),question.getQuestionStem(),options,answer);
    }


    //编辑修改试卷
    @Override
    public int updateQuestion(QuestionVo question) {
        log.info("修改试卷：{}",question);
        String options = "";
        if (question.getOptions().length > 0) {
            options = question.getOptions()[0];
            for (int i = 1; i < question.getOptions().length; i++) {
                options += "," + question.getOptions()[i];
            }
        }

        String answer = "";
        if (question.getAnswer() instanceof String) { //单选，instanceof是否是String型
            answer = (String) question.getAnswer();
        } else if (question.getAnswer() instanceof Integer) { //判断
            answer = (int) question.getAnswer() + "";
        } else {
            List list = (List) question.getAnswer();
            answer = list.get(0).toString();
            for (int i = 1; i < list.size(); i++) {
                answer += "," + list.get(i).toString();
            }
        }
        QuestionPo questionPo = new QuestionPo();
        questionPo.setQuestionStem(question.getQuestionStem());
        questionPo.setAnswer(answer);
        questionPo.setEid(question.getEid());
        questionPo.setOptions(options);
        questionPo.setTid(question.getTid());
        questionPo.setQid(question.getQid());

        return questionMapper.updateQuestion(questionPo);
    }

    //批量删除试卷
    @Override
    public int delQuestions(String qids) {
        log.info("批量删除试卷：{}",qids);
        return questionMapper.delQuestions(qids);
    }


}